<template>
  <view v-if="goodsIfo.goods_name" >
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular='true'>
      <swiper-item v-for="(item,i) in goodsIfo.pics" :key="i">
        <image :src="item.pics_big" mode="" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods-ifo-box">
      <!-- 商品价格 -->
      <view class="goods-price">
        ￥{{goodsIfo.goods_price}}
      </view>
      <!-- 商品详细信息 -->
      <view class="goods-ifo">
        <view class="goods-name">
          {{goodsIfo.goods_name}}
        </view>
        <view class="goods-favi">
          <uni-icons type="star" size="18"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="transtform-price">
        快递：免运费--{{cart.length}}
      </view>
    </view>
    <!-- 商品详细信息 -->
   <view class="goods-ifo-container">
      <rich-text :nodes="goodsIfo.goods_introduce"></rich-text>
   </view>
    <!-- d底部导航 -->
    <view class="buttom-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    data() {
      return {
        goodsIfo: {},
        options: [ {
          icon: 'shop',
          text: '店铺',
          info: 0,
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    methods: {
      ...mapMutations('m_cart',['addToCart']),
      async getgoodsIfo(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail?', {
          goods_id
        })
        if (res.meta.status !== 200) {
          return uni.showMsg(res.meta.msg)
        }
        //正则替换图片，将图片加上display属性
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, '<img style="display:block"')
          .replace(/webp/g, 'jpg')
        this.goodsIfo = res.message
        console.log(res)
      },
      preview(i) {
        uni.previewImage({
          current: i,
          loop: true,
          urls: this.goodsIfo.pics.map(x => x.pics_big)
        })
      },
      onClick(e) {
        console.log (e)
        if(e.content.text==='购物车'){
          uni.switchTab({
            url:"../../pages/cart/cart"
          })
        }
      },
      buttonClick(e) {
        if(e.content.text==='加入购物车'){
          const goods={
            goods_id:this.goodsIfo.goods_id,
            goods_name:this.goodsIfo.goods_name,
            goods_price:this.goodsIfo.goods_price,
            goods_count:1,
            goods_small_logo:this.goodsIfo.goods_small_logo,
            goods_state:true
          }
          this.addToCart(goods)
        }
      }
    },
    computed:{
      ...mapState('m_cart',['cart']),
      ...mapGetters('m_cart',['total'])
    },
    watch:{
      /* total(newVal){
        const findResult=this.options.find((x)=>x.text==='购物车')
        if(findResult){
          findResult.info=newVal
        }
      } */
      total:{
        handler(newVal){
          const findResult=this.options.find((x)=>x.text==='购物车')
          if(findResult){
            findResult.info=newVal
          }
        },
        immediate:true
      }
    },
    onLoad(options) {
      const goods_id = options.good_id
      this.getgoodsIfo(goods_id)
      // console.log(options)
    }
  }
</script>

<style lang="scss">
  
  swiper {
    height: 450rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-ifo-box {
    padding: 20rpx;
    padding-right: 0;

    .goods-price {
      font-size: 36rpx;
      color: #c00000;
      margin: 20rpx 0;
    }

    .goods-ifo {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 26rpx;
        margin-right: 20rpx;
      }

      .goods-favi {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 240rpx;
        font-size: 24rpx;
        border-left: 4rpx solid #efefef;
        color: gray;
      }
    }

    .transtform-price {
      margin-top: 20rpx;
      font-size: 24rpx;
      color: gray;
    }
   
  }
  .goods-ifo-container{
    padding-bottom: 100rpx;
  }
  .buttom-nav{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
